<?php
/**
 * Auto.php
 * Created by PhpStorm.
 * User: 江小波
 * Date: 2019/06/11
 * Time: 21:57
 */
namespace app\console\command;

use app\console\command\auto\tpl\Logic;
use app\console\command\auto\tpl\Model;
use think\console\input\Option;

class Auto extends Base
{
    protected function getCmdName()
    {
        return 'auto';
    }
    protected function getCmdDescription()
    {
        return 'auto cmd';
    }
    protected function processResult($result)
    {
        return $result;
    }
    protected function applyOption()
    {
        // 添加可配置参数，不传则为null
        $this->addOption('dir',null,Option::VALUE_OPTIONAL,'dir');
        $this->addOption('name',null,Option::VALUE_OPTIONAL,'name');
        $this->addOption('table',null,Option::VALUE_OPTIONAL,'table');
        $this->addOption('database',null,Option::VALUE_OPTIONAL,'database');
    }

    /**
     * 命令行执行
     */
    // php cmd auto --action generateTestData
    public function generateTestData()
    {
        // 创建支付方式
        $data = [
            'is_enable' => '1',
            'name' => '积分支付',
            'sort' => '0',
            'payment' => 'Integral',
            'desc' => '积分支付',
            'apply_terminal' => json_encode(['pc','wap','app','alipay','weixin','baidu'])
        ];
        $save = new \app\api\open\payment\Save($data);
        $payment = $save->index();
    }
    // php cmd auto --action openApi --dir dir/ --name member_address --database php_system
    // php cmd auto --action openApi --dir dir/ --table s_member_address --database php_system
    // php cmd auto --action openApi --name member_address --database php_system
    // php cmd auto --action openApi --dir member/ --name order --database php_system
    public function openApi($params,$options)
    {
        if(empty($options['database'])){
            $database = config('database.database');
        }else{
            $database = $options['database'];
        }
        if(empty($options['dir'])) {
            $dir = "";
        }else{
            $dir = $options['dir'];
        }
        if(empty($options['name'])) {
            if(empty($options['table'])) {
                exit("name or table error");
            } else {
                $method = 'table';
                $table = $options['table'];
            }
        } else {
            if(empty($options['table'])) {
                $method = 'name';
                $table = $options['name'];
            } else {
                $method = 'table';
                $table = $options['table'];
            }
        }
        Model::template($method,$table);
        Logic::template($options['dir'],$method,$table,$database);
    }
}